import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import DetectUpload from '../views/DetectUpload.vue';
import DetectResult from '../views/DetectResult.vue';

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue')
    },
    {
        path: '/register', // 注册页路径
        name: 'Register',
        component: () => import('@/views/Register.vue') // 引入注册组件
    },
    {
        path: '/',
        redirect: '/login' // 默认重定向到登录页
    },
    { path: '/detect/upload', component: DetectUpload, meta: { requiresAuth: true } }, // 需要登录
    { path: '/detect/result', component: DetectResult, meta: { requiresAuth: true } },
    {
        path: '/home',
        component: () => import('../views/Home.vue'), // 确保路径正确
        meta: { requiresAuth: true } // 需要登录
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})
// 路由守卫：未登录用户跳转到登录页
router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth && !localStorage.getItem('token')) {
        next('/login');
    } else {
        next();
    }
});

export default router